using System.Collections.Generic;
using UnityEngine;

public class MatchController : MonoBehaviour, MatchControllerUiInteface
{
	protected List<PlayerBase> players = new List<PlayerBase>();

	protected GameStartScript starter;

	public Ball ball { get; protected set; }

	public CameraController cameraController
	{
		get
		{
			return starter.cameraController;
		}
	}

	public bool gameDone { get; protected set; }

	public SingleTableTennis gameType { get; protected set; }

	public MatchUI ui { get; protected set; }

	public virtual void Init(GameStartScript starter, MatchUI ui, SingleTableTennis gameType)
	{
		this.starter = starter;
		ball = starter.CreateBall();
		ball.onCollision = OnBallHitObject;
		this.ui = ui;
		this.gameType = gameType;
		gameType.Init(this, ui, MatchParameters.InitParameters.pointsToWinGame, MatchParameters.InitParameters.gamesToWinMatch);
		CreatePlayers();
		StartGame();
	}

	public virtual void StartGame()
	{
		gameType.StartMatch();
	}

	public void CreatePlayers()
	{
		HumanPlayer humanPlayer = starter.CreateHumanPlayer();
		humanPlayer.Init(players.Count, -1, this);
		players.Add(humanPlayer);
		AiPlayer aiPlayer = starter.CreateAiPlayer();
		aiPlayer.Init(players.Count, 1, this);
		players.Add(aiPlayer);
	}

	public PlayerBase Other(PlayerBase player)
	{
		return players[(player.teamTag + 1) % players.Count];
	}

	public PlayerBase GetPlayer(int teamTag)
	{
		return players[teamTag];
	}

	public virtual void StartServe(PlayerBase player)
	{
		Debug.Log("MatchController.StartServe " + player.teamTag);
		ball.isBallInGame = false;
		player.StartServe(ball);
		Other(player).ReceiveServe();
	}

	public virtual void OnPlayerHitBall(PlayerBase player, HitBallParams hitBallParams)
	{
		gameType.OnPlayerHitBall(player);
		Other(player).ReceiveBall(ball);
	}

	public virtual void OnBallHitObject(Vector3 pos, HitObjectTag hitObjectTag)
	{
		gameType.OnBallHitObject(pos, hitObjectTag);
	}

	public virtual void AwardPoint(AwardPointParams awardPointParams)
	{
		gameType.ContinueFromLastPoint(awardPointParams);
	}

	public virtual void OnGameWon(bool isPlayerWinner)
	{
		gameDone = true;
		float num = 0f;
		int num2 = 0;
		MultiplayerGameDone.GameParams gameParams = MultiplayerGameDone.GameParams.FromMatchParameters().SetWinnings(num2).HidePlayerStars();
		ui.gameDone.DisableButtons();
		GGPlayerSettings instance = GGPlayerSettings.instance;
		GameDoneListener gameDoneListener = MatchParameters.InitParameters.gameDoneListener;
		if (gameDoneListener != null)
		{
			gameDoneListener.OnGameDone(this);
		}
		if (num > 0f)
		{
			float num3 = instance.LevelProgress();
			int num4 = instance.Level();
			string l = instance.LevelName();
			instance.walletManager.AddCurrency(CurrencyType.coins, num2);
			if (num3 < (float)instance.Level())
			{
				Analytics.instance.PlayerReachedNewLevel();
			}
			float curLevel = instance.LevelProgress();
			string l2 = instance.LevelName();
			ui.gameDone.ShowAnim(gameParams.SetWinnerIndex((!isPlayerWinner) ? 1 : 0).SetLevelNames(l, l2).SetLevels(num3, curLevel)
				.SetDelegate(delegate
				{
					Debug.Log("Done");
					OnGameWonComplete();
				}));
			Debug.Log("score report");
			GameNetwork.instance.reportScore(instance.Model.score);
		}
		else
		{
			float num5 = instance.LevelProgress();
			int num6 = instance.Level();
			float curLevel2 = num5;
			string text = instance.LevelName();
			ui.gameDone.ShowAnim(gameParams.SetWinnerIndex((!isPlayerWinner) ? 1 : 0).SetLevelNames(text, text).SetLevels(num5, curLevel2)
				.SetDelegate(delegate
				{
					Debug.Log("Done");
					OnGameWonComplete();
				}));
		}
		GGPlayerSettings.instance.Model.shownTutorial = true;
		GGPlayerSettings.instance.Save();
	}

	private void OnGameWonComplete()
	{
		Time.timeScale = 1f;
		BehaviourSingleton<ScreenNavigation>.instance.LoadMain(MenuParameters.MenuParams.ShowScreen.None);
	}

	public virtual void MatchInterfaceOnSendMessage(string message)
	{
	}

	public virtual void MatchInterfaceOnRematch()
	{
	}

	public virtual void MatchInterfaceOnPause()
	{
		if (!gameDone)
		{
			Time.timeScale = 0f;
		}
	}

	public virtual void MatchInterfaceOnResume(bool exit)
	{
		if (exit)
		{
			OnGameWonComplete();
		}
		else
		{
			Time.timeScale = starter.timeScale;
		}
	}

	TableTennisGameTypeBase MatchControllerUiInteface.GetGameType()
	{
		return null;
	}

	void MatchControllerUiInteface.OnPause()
	{
		MatchInterfaceOnPause();
	}

	void MatchControllerUiInteface.OnResume(bool exit)
	{
		MatchInterfaceOnResume(exit);
	}

	void MatchControllerUiInteface.OnRematch()
	{
		MatchInterfaceOnRematch();
	}

	void MatchControllerUiInteface.OnSendMessage(string message)
	{
		MatchInterfaceOnSendMessage(message);
	}

	bool MatchControllerUiInteface.isGameWon()
	{
		return gameDone;
	}
}
